﻿Imports System.Linq

Public Class CategoryMenu
    Inherits System.Web.UI.UserControl

#Region "Khai báo các event"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.CreateMenu()
        Catch ex As Exception
            ''Giau loi
            'Server.Transfer("~/Pages/Error.aspx")
        End Try
    End Sub

#End Region

#Region "Khai báo các function medthod"

    Public Sub CreateMenu()
        Dim dataAccess As DBAccess
        Dim ds As DataSet
        Dim xSql As String = String.Empty
        Dim xMenu As String = String.Empty

        Try
            dataAccess = New DBAccess(ConfigurationManager.ConnectionStrings("ConStr").ConnectionString)

            'xSQL = "SELECT M.MENU_A, C.NAME, C.LINK_NAME FROM T_MENU AS M, T_CATEGORY AS C WHERE M.MENU_B = 0 AND M.ITEM_CODE = C.CODE "
            'dsMenuA = dataAccess.GetDBDataset(xSQL)

            'If dsMenuA Is Nothing OrElse dsMenuA.Tables(0).Rows.Count < 1 Then
            '    Throw New Exception("NO_DATA_FOUND")
            'End If
            'For Each menuA In dsMenuA.Tables(0).Rows

            '    xMenu = xMenu & "<h3>" & menuA("NAME").ToString() & "</h3>"
            '    xMenu = xMenu & "<div>"

            '    xSQL = "SELECT MA.NAME, MA.LINK_NAME FROM T_MENU AS M, T_MANUFACTORY AS MA WHERE M.MENU_A = @MENU_A AND M.MENU_B <> 0 AND M.ITEM_CODE = MA.CODE "
            '    xSQL = xSQL.Replace("@MENU_A", CInt(menuA("MENU_A")))
            '    Dim dsMenuB = dataAccess.GetDBDataset(xSQL)

            '    If dsMenuB Is Nothing Then
            '        Throw New Exception("NO_DATA_FOUND")
            '    End If
            '    Dim xMenuB As String = String.Empty
            '    For Each menuB In dsMenuB.Tables(0).Rows
            '        xMenuB = xMenuB & "<div class='menu'><a href='" & ResolveUrl("~/" & menuA("LINK_nAME") & "/" & menuB("LINK_NAME")) & "/'>" & menuB("NAME") & "</a></div>"
            '    Next
            '    xMenu = xMenu & xMenuB & "</div>"

            'Next

            xSql = ""
            xSql = xSql & vbCrLf & "SELECT"
            xSql = xSql & vbCrLf & "        MENU_A"
            xSql = xSql & vbCrLf & ",       MAX(NAME) AS NAME"
            xSql = xSql & vbCrLf & ",       MAX(LINK_NAME) AS LINK_NAME"
            xSql = xSql & vbCrLf & "FROM"
            xSql = xSql & vbCrLf & "        T_MENU"
            xSql = xSql & vbCrLf & "        LEFT JOIN T_CATEGORY"
            xSql = xSql & vbCrLf & "            ON MENU_A = CODE"
            xSql = xSql & vbCrLf & "GROUP BY"
            xSql = xSql & vbCrLf & "        MENU_A"
            xSql = xSql & vbCrLf & ";"
            xSql = xSql & vbCrLf & "SELECT"
            xSql = xSql & vbCrLf & "        MENU_A"
            xSql = xSql & vbCrLf & ",       MENU_B"
            xSql = xSql & vbCrLf & ",       NAME"
            xSql = xSql & vbCrLf & ",       LINK_NAME"
            xSql = xSql & vbCrLf & "FROM"
            xSql = xSql & vbCrLf & "        T_MENU"
            xSql = xSql & vbCrLf & "        LEFT JOIN T_MANUFACTORY"
            xSql = xSql & vbCrLf & "            ON MENU_B = CODE"

            ds = dataAccess.GetDBDataset(xSql)

            For Each menuA In ds.Tables(0).Rows
                xMenu = xMenu & "<h3>" & menuA("NAME").ToString() & "</h3>"
                xMenu = xMenu & "<div>"
                Dim xMenuB As String = String.Empty
                For Each menuB In ds.Tables(1).AsEnumerable.Where(Function(x) x("MENU_A") = menuA("MENU_A"))
                    xMenuB = xMenuB & "<div class='menu'><a href='" & ResolveUrl("~/" & menuA("LINK_nAME") & "/" & menuB("LINK_NAME")) & "/'>" & menuB("NAME") & "</a></div>"
                Next
                xMenu = xMenu & xMenuB & "</div>"
            Next

            ltrMenu.Text = xMenu
        Catch ex As Exception
            Throw
        End Try
    End Sub

#End Region

End Class